package com.feicheng.house.wcpay.serviceImpl;

import java.util.Map;
import org.springframework.stereotype.Service;

import com.feicheng.house.exception.SystemException;
import com.feicheng.house.result.ResultInfo;
import com.feicheng.house.wcpay.wechatConfig;
import com.feicheng.house.wcpay.service.UserAuthorizationService;
import com.google.gson.Gson;
import java.util.HashMap;

import util.HttpClientUtils;
@Service
public class UserAuthorizationServiceImpl implements UserAuthorizationService {

	@Override
	public ResultInfo getOpenId(String code, String state) {
		String accessTokenUrl = wechatConfig.GATEWAY + "?appid=" + wechatConfig.APPID
				+"&appsecret="+ wechatConfig.APP_SECRET
				+"&code="+code
				+"&grant_type=authorization_code";
		String result = null;
		try {
			//get方法抛出了异常
			result = new HttpClientUtils().get(accessTokenUrl);
			 Gson gson = new Gson();
	            Map<String,Object> resultMap = gson.fromJson(result, HashMap.class);

	            //解析微信用户获得唯一凭证openId
	            String openid = (String)resultMap.get("openid");
	            //TODO 将openid放入到session中
	            //TODO 将openid存入数据库的student表的openid字段
	            System.out.println(openid);
	            //封装返回
	            return ResultInfo.success(resultMap);
		} catch (Exception e) {
			throw new SystemException("");
		}
		
	}
	

}
